new Vue({
	el:'#app',
	data() {
		return {
			// “用例数据包路径选择”对话框
			packagePath: '',
			dialogVisible: false,
			dialogText: '未设置“用例数据包”路径，请设置并提交！',
			// “执行确认”对话框
			dialogCases: false,
			// 目录树
			filterText: '',
			data: [],
			defaultProps: {children: 'children', label: 'label'},
			getSetArray: [],
			// 功能表单
			form: {
				browser: 'Chrome', // 浏览器平台
				url: 'http://localhost/', // 测试环境地址
				implicitlyWait: 10, // 隐式等待时长
				user: 'admin', // 测试角色用户名
				password: '', // 测试角色密码
				pluginName: 'oa-class', // 辅助插件
				interfaceSwitch: true, // 接口脚本运行开关
				codeSwitch: false, // 验证码识别运行开关
				retrySwitch: false // 用例出错二次重试运行开关
			}
		}
	},
	// 生命钩子 -- 挂载完成之后
	mounted() {
		var that = this;
		const loading = that.$loading({target: window.document.querySelector('#section-box'), lock: true, text: '正在获取用例数据'});  // 加载动画
		async function init(){
			let path = await eel.get_path()();
			console.log(path)
			if (path) {
				// 存在包路径
				that.packagePath = path;
				try{
					let pathJson = await eel.scan_clean(path)();
					let content = await eel.get_hierarchy(pathJson['CASES_FOLDER'], [])();
					that.data = content;  // 赋值
				}catch (e){
					that.$message.error('获取失败，请检查“数据包”是否符合规定要求！');
				}
			}else{
				// 不存在包路径
				that.dialogVisible = true;
			}
		};
		init();  // 调用
		loading.close();  // 关闭动画
	},
	watch: {
		filterText(val) {
			this.$refs.tree.filter(val);
		}
	},
	methods:{
		// 资源管理器打开事件
		async openWindows() {
			var that = this;
			let packagePath = await eel.set_explorer()()
			if (packagePath) {
				that.packagePath = packagePath;
			}else{
				that.$message.error('资源管理器打开失败！');
			};
		},
		// 设置“用例数据包”路径事件
		async submitInformation() {
			var that = this;
			if (that.packagePath) {
				let pathLevel = await eel.set_path(that.packagePath)();
				console.log(pathLevel)
				if (pathLevel) {
					console.log('设置成功！');
					that.packagePath = pathLevel;
					that.dialogVisible = false;
					window.location.reload(true);  // 刷新
				}else{
					that.$message.error('设置失败！');
				}
			}else{
				this.$message({
					message: '警告，用例数据包路径不可为空！',
					type: 'warning'
				});
			};
		},
		// 开始执行事件
		async onRun() {
			var that = this;
			window.close();  // 窗口关闭
			await eel.run_start(that.packagePath, that.getSetArray, that.form)()
		},
		// 目录树检索事件
		filterNode(value, data) {
			if (!value) return true;
			return data.label.indexOf(value) !== -1;
		},
		// 目录树节点选中事件
		onChoice(data) {
			var that = this;
			var childsIds = that.$refs.tree.getCheckedKeys();
			var childsNodes = that.$refs.tree.getCheckedNodes();
			var tranChildsNodes = [];
			for (var i = 0; i < childsIds.length; i++) {
				if(childsIds[i].match('.xlsx') || childsIds[i].match('.xls')) {
					tranChildsNodes.push(childsNodes[i]['file']);
				}
			}
			// 处理
			that.getSetArray = tranChildsNodes;
			console.log(that.getSetArray);
		},
		// 功能表单提交事件
		onSubmit(element) {
			var that = this;
			console.log(that.form);
			if (that.getSetArray.length) {
				if (that.form.browser && that.form.url && that.form.implicitlyWait && that.form.user && that.form.pluginName) {
					that.dialogCases = true;
				}else{
					that.$message({
						message: '警告，除测试用户密码外的所有必填项不可为空！',
						type: 'warning'
					});
				}
			}else{
				that.$message({
					message: '警告，请选择要运行的测试用例文件！',
					type: 'warning'
				});
			}
		},
		// 取消执行事件
		async onTermination() {
			var that = this;
			window.close();  // 窗口关闭
			await eel.run_termination()(); // 进程关闭
		},
		// 用例数据包路径修改事件
		async setPath() {
			var that = this;
			that.dialogText = "打开资源管理器以设置“用例数据包”路径！"
			let path = await eel.get_path()();
			console.log(path)
			if (path) {
				that.packagePath = path;  // 赋值
			}
			that.dialogVisible = true;
		}
	}
})